package org.microframework.cloud.logs.service.impl;

import java.util.Date;
import org.microframework.base.core.web.page.PageQuery;
import org.microframework.cloud.logs.model.domain.SystemLog;
import org.microframework.cloud.logs.service.ISystemLogService;
import org.springframework.stereotype.Service;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

/**
 * 系统日志服务实现类
 * 
 * @author zs
 * @version 1.0.0
 * @date 2024-05-21
 */
@Service
public class SystemLogServiceImpl implements ISystemLogService {

    @Override
    public boolean insertSystemLog(SystemLog systemLog) {
        try {
            systemLog.setCreateTime(new Date());
            systemLog.setUpdateTime(new Date());
            return systemLog.save();
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean info(String serviceName, String content) {
        SystemLog log = new SystemLog();
        log.setServiceName(serviceName);
        log.setContent(content);
        log.setLevel("INFO");
        log.setStatus(0);
        log.setOperTime(new Date());
        return insertSystemLog(log);
    }

    @Override
    public boolean warn(String serviceName, String content) {
        SystemLog log = new SystemLog();
        log.setServiceName(serviceName);
        log.setContent(content);
        log.setLevel("WARN");
        log.setStatus(0);
        log.setOperTime(new Date());
        return insertSystemLog(log);
    }

    @Override
    public boolean error(String serviceName, String content, Throwable exception) {
        SystemLog log = new SystemLog();
        log.setServiceName(serviceName);
        log.setContent(content);
        log.setLevel("ERROR");
        log.setStatus(1);
        log.setException(exception.getMessage());
        log.setOperTime(new Date());
        return insertSystemLog(log);
    }

    @Override
    public boolean fatal(String serviceName, String content, Throwable exception) {
        SystemLog log = new SystemLog();
        log.setServiceName(serviceName);
        log.setContent(content);
        log.setLevel("FATAL");
        log.setStatus(1);
        log.setException(exception.getMessage());
        log.setOperTime(new Date());
        return insertSystemLog(log);
    }

    @Override
    public Page<SystemLog> selectSystemLogList(SystemLog systemLog, PageQuery<SystemLog> pageRequest) {
        StringBuilder sql = new StringBuilder("from " + SystemLog.TABLE_NAME + " where 1=1");
        
        if (systemLog != null) {
            if (systemLog.getServiceName() != null && !systemLog.getServiceName().isEmpty()) {
                sql.append(" and service_name like '%").append(systemLog.getServiceName()).append("%'");
            }
            if (systemLog.getLevel() != null && !systemLog.getLevel().isEmpty()) {
                sql.append(" and level = '").append(systemLog.getLevel()).append("'");
            }
            if (systemLog.getStatus() != null) {
                sql.append(" and status = ").append(systemLog.getStatus());
            }
        }
        
        String orderBy = pageRequest.getOrderBy();
        String orderDirection = pageRequest.getOrderDirection();
        if (orderBy != null && !orderBy.isEmpty()) {
            sql.append(" order by ").append(orderBy);
            if (orderDirection != null && !orderDirection.isEmpty()) {
                sql.append(" ").append(orderDirection);
            }
        } else {
            sql.append(" order by oper_time desc");
        }
        
        return SystemLog.dao.paginate(pageRequest.getPageNumber(), pageRequest.getPageSize(), 
            "select *", sql.toString());
    }

    @Override
    public int deleteSystemLogByIds(String[] ids) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        StringBuilder sql = new StringBuilder("delete from " + SystemLog.TABLE_NAME + " where id in (");
        for (int i = 0; i < ids.length; i++) {
            sql.append("'").append(ids[i]).append("'");
            if (i < ids.length - 1) {
                sql.append(",");
            }
        }
        sql.append(")");
        return Db.update(sql.toString());
    }

    @Override
    public SystemLog selectSystemLogById(String id) {
        return SystemLog.dao.findById(id);
    }

    @Override
    public boolean cleanSystemLog() {
        try {
            Db.update("truncate table " + SystemLog.TABLE_NAME);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
} 